﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LDAPOfficeTool
{
    public partial class Org2DisplayUniversal : UserControl
    {
        LDAPServiceNS.OrganisationBean Org2Display;
        string OuTop;
        Microsoft.Office.Interop.Word.Document Doc2Handle;  // handle to document to be processed
        const int ExpandedHeightLimit = 115;
        const int ContractedHeightLimit = 48;
        // auf welche länge wird der Name für die Ausgabe in die Minimaske abgeschnitten
        const int MaxNameLengthLimit = 33;


        bool PhysicalAddressExists()
        {
            if (Org2Display.gvPhysicalAddress != null)
            {
                return true;
            }
            return false;
        }


        string GetPhysicalAddress()
        {
            string RetVal;
            string[] AddressArr;
            AddressArr = Org2Display.gvPhysicalAddress.Split('$');
            //                org2Display.postal
            RetVal = AddressArr[0] + ", ";
            for (int i = 1; i < AddressArr.Length; i++)
            {
                RetVal = RetVal + AddressArr[i];
            }
            return RetVal;
        }


        bool PostalAddressExists()
        {
            if (Org2Display.postalAddress != null)
            {
                return true;
            }
            return false;
        }


        string GetPostalAddress()
        {
            string RetVal;
            string[] AddressArr;
            AddressArr = Org2Display.postalAddress.Split('$');
            RetVal = AddressArr[0];
            for (int i = 1; i < AddressArr.Length; i++)
            {
                RetVal = RetVal + ", " + AddressArr[i];
            }
            return RetVal;
        }


        string GetPostalAddressX()
        {
            string RetVal;
            string []AddressArr;
            RetVal = "?";
            if (Org2Display.gvPhysicalAddress != null)
            {
                 AddressArr = Org2Display.gvPhysicalAddress.Split('$');
                RetVal = "PH: " + StringUtils.StringStorage2StringWithDelimiter(AddressArr, " ");

            }
            else
            {
                if (Org2Display.postalAddress != null)
                {
                    AddressArr = Org2Display.postalAddress.Split('$');
                    RetVal = "PO: " + StringUtils.StringStorage2StringWithDelimiter(AddressArr, " ");
                }
                else
                {
                    MessageBox.Show("Nothing at all of address for organisation");
                    RetVal = " - ";
                }
            }
            return RetVal;
        }


        public string GetOrgDescription()
        {
            string RetVal;
            string DiagnosticsTxt;
            if (Org2Display.description != null)
            {
                DiagnosticsTxt = "Getting org description, data length: " + Org2Display.description.Length.ToString();
                for (int i =0; i < Org2Display.description.Length; i++)
                {
                    DiagnosticsTxt = DiagnosticsTxt + Environment.NewLine + i.ToString () + ":  " + Org2Display.description[i];
                }
                RetVal = StringUtils.StringStorage2String(Org2Display.description);
            }
            else
            {
                RetVal = "-";
            }
            return RetVal;
        }


        public Org2DisplayUniversal(LDAPServiceNS.OrganisationBean pOrg, string psOuTop, Microsoft.Office.Interop.Word.Document pdoc2Handle)
        {
            InitializeComponent();
            Org2Display = pOrg;
            OuTop = psOuTop;
            lblEmail.Text = Org2Display.mail;
            bool CommonNameSet;
            CommonNameSet = false;
            // BBB09 take care of gvcn
            if (Org2Display.gvCn != null)
            {
                if (Org2Display.gvCn.Length > 0)
                {
                    // use it now
                    CommonNameSet = true;
                    linklblLDAP.Text = StringUtils.CutOffWithTail(Org2Display.gvCn, "...", MaxNameLengthLimit);
                }
            }
            if (!CommonNameSet)
            {
                // set it as usual
                linklblLDAP.Text = StringUtils.CutOffWithTail(Org2Display.cn[0], "...", MaxNameLengthLimit);
            }
            // BBB09
            if (PhysicalAddressExists())
            {
                lblAddress.Text = GetPhysicalAddress();
                toolTipAddress.SetToolTip(lblAddress, "Besuchsadresse");
            }
            else
            {
                if (PostalAddressExists())
                {
                    lblAddress.Text = GetPostalAddress();
                    toolTipAddress.SetToolTip(lblAddress, "Postanschrift");

                }
                else
                {
                    lblAddress.Text = "-";
                    toolTipAddress.SetToolTip(lblAddress, "weder Besuchadresse noch Postanschrift vorhanden");

                }
            }
            lblMainOrg.Text = OuTop;
            Doc2Handle = pdoc2Handle; 
            btnExpand.Visible = true;
            btnContract.Visible = false;
            this.Height = ContractedHeightLimit;
        }


        private void btnExpand_Click(object sender, EventArgs e)
        {
            this.Height = ExpandedHeightLimit;
            btnContract.Visible = true;
            btnExpand.Visible = false;
            btnEinfuegen.Visible = true;
//            MessageBox.Show("Text: " + this.subBrowser.DocumentText);
        }


        private void btnContract_Click(object sender, EventArgs e)
        {
            this.Height = ContractedHeightLimit;
            btnContract.Visible = false;
            btnExpand.Visible = true;
            btnEinfuegen.Visible = false;
        }

        
        private void eMailAdresseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Selection CurrentDocSelection;
            CurrentDocSelection = Doc2Handle.Application.Selection;
            CurrentDocSelection.TypeText(Org2Display.mail);
        }

        private void linklblLDAP_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string Link2Open;
            BrowserForm Org2LDAP;
            Org2LDAP = new BrowserForm();
            Link2Open = "http://ldap.gv.at/lfrz.at/ldapw/index.php?sPage=1&showDetailDn=" + Org2Display.dn + "&detailType=organisation";
            Org2LDAP.SetUri(Link2Open);
            Org2LDAP.Visible = true;
//            MessageBox.Show("link: " + sLink2Open);
        }

 
        private void orgEinheitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Text2Enter;
            Text2Enter = "";
// 2011.06.29 gerold sagt, gvCn hat vorrang vorm cn; wenn leer, dann cn nehmen
            if (Org2Display.gvCn != null)
            {
                if (Org2Display.gvCn.Length > 0)
                {
                    Text2Enter = Org2Display.gvCn;
                }
            }
            if (Text2Enter.Length == 0)
            {
                Text2Enter = StringUtils.StringStorage2String(Org2Display.cn);
            }
            Microsoft.Office.Interop.Word.Selection CurrentDocSelection;
            CurrentDocSelection = Doc2Handle.Application.Selection;
            CurrentDocSelection.TypeText(Text2Enter);
        }


        private void aufgabeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Text2Enter;
            Text2Enter = GetOrgDescription();
            Microsoft.Office.Interop.Word.Selection CurrentDocSelection;
            CurrentDocSelection = Doc2Handle.Application.Selection;
            CurrentDocSelection.TypeText(Text2Enter);
        }


        private void btnEinfuegen_Click(object sender, EventArgs e)
        {
            Point AuxPoint;
            AuxPoint = new Point(5, 5);
            contextMenuStripOActions.Show(btnEinfuegen, AuxPoint);
        }


        private void ouBezeichnung_Click(object sender, EventArgs e)
        {
            string Text2Enter;
            //
            Text2Enter = StringUtils.StringStorage2String(Org2Display.cn);
            Microsoft.Office.Interop.Word.Selection Selection;
            Selection = Doc2Handle.Application.Selection;
            Selection.TypeText(Text2Enter);
        }


        private void anschriftMenuStripMenuItem_Click(object sender, EventArgs e)
        {
            bool Found;
            string Text2Enter;
            string AddressCandidate;
            AddressCandidate = "";
            Found = false;
            //
            if (PostalAddressExists())
            {
                AddressCandidate = GetPostalAddress();
                if (AddressCandidate.Length > 4)
                {
                    Found = true;
                }
            }

            if (!Found)
            {
                if (PhysicalAddressExists())
                {
                    AddressCandidate = GetPhysicalAddress();
                    if (AddressCandidate.Length > 4)
                    {
                        Found = true;
                    }
                }
            }
            if (Found)
            {
// 2011.06.29 gerold hat gemerkt, dass manche Adressen verkehrt sind; falsches Attrib verwendet
//                Text2Enter = StringUtils.StringStorage2String(Org2Display.cn) + Environment.NewLine + AddressCandidate;
                Text2Enter = "";
                if (Org2Display.gvCn != null)
                {
                    if (Org2Display.gvCn.Length > 0)
                    {
                        Text2Enter = Org2Display.gvCn;
                    }
                }
                if (Text2Enter.Length == 0)
                {
                    Text2Enter = StringUtils.StringStorage2String(Org2Display.cn);
                }
                Text2Enter = Text2Enter +Environment.NewLine + AddressCandidate;
                Microsoft.Office.Interop.Word.Selection vSelection;
                //            org2Display.
                vSelection = Doc2Handle.Application.Selection;
                vSelection.TypeText(Text2Enter);
            }
        }
    }
}
